<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>caffe训练自己的数据集并用python接口预测 | Jin Tian</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="深度学习" />
  
  
  
  
  <meta name="description" content="Caffe训练自己的数据集并用Python接口预测 本教程作者是在读硕士金天童鞋，在当地较为英俊的男子，大家对教程有任何疑问欢迎联系我：WeChatjintianiloveu，同时也欢迎大家转载评论，不过请保留这段版权信息，桑口～  Caffe安装首先还是简要的说一下Caffe在Ubuntu下的安装过程，具体安装过程如下：  安装前置依赖前置依赖项较多，好在Ubuntu下都可以通过apt下载安装，">
<meta name="keywords" content="深度学习">
<meta property="og:type" content="article">
<meta property="og:title" content="Caffe训练自己的数据集并用Python接口预测">
<meta property="og:url" content="http://yoursite.com/2017/02/02/2017-1-6-Caffe-训练自己的数据集并用Python接口预测和批量预测/index.html">
<meta property="og:site_name" content="Jin Tian">
<meta property="og:description" content="Caffe训练自己的数据集并用Python接口预测 本教程作者是在读硕士金天童鞋，在当地较为英俊的男子，大家对教程有任何疑问欢迎联系我：WeChatjintianiloveu，同时也欢迎大家转载评论，不过请保留这段版权信息，桑口～  Caffe安装首先还是简要的说一下Caffe在Ubuntu下的安装过程，具体安装过程如下：  安装前置依赖前置依赖项较多，好在Ubuntu下都可以通过apt下载安装，">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-02-03T02:51:20.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Caffe训练自己的数据集并用Python接口预测">
<meta name="twitter:description" content="Caffe训练自己的数据集并用Python接口预测 本教程作者是在读硕士金天童鞋，在当地较为英俊的男子，大家对教程有任何疑问欢迎联系我：WeChatjintianiloveu，同时也欢迎大家转载评论，不过请保留这段版权信息，桑口～  Caffe安装首先还是简要的说一下Caffe在Ubuntu下的安装过程，具体安装过程如下：  安装前置依赖前置依赖项较多，好在Ubuntu下都可以通过apt下载安装，">
  
    <link rel="alternate" href="/atom.xml" title="Jin Tian" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="124px" height="124px" alt="Hike News" src="/css/images/mylogo.jpg">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li> <a class="main-nav-link" href="http://luoli-luoli.com/chat">chat</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-2017-1-6-Caffe-训练自己的数据集并用Python接口预测和批量预测" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Caffe训练自己的数据集并用Python接口预测
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2017/02/02/2017-1-6-Caffe-训练自己的数据集并用Python接口预测和批量预测/" class="article-date">
	  <time datetime="2017-02-01T18:31:45.000Z" itemprop="datePublished">2017-02-02</time>
	</a>

      
      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="Caffe训练自己的数据集并用Python接口预测"><a href="#Caffe训练自己的数据集并用Python接口预测" class="headerlink" title="Caffe训练自己的数据集并用Python接口预测"></a>Caffe训练自己的数据集并用Python接口预测</h3><blockquote>
<p>本教程作者是在读硕士金天童鞋，在当地较为英俊的男子，大家对教程有任何疑问欢迎联系我：WeChat<br><code>jintianiloveu</code>，同时也欢迎大家转载评论，不过请保留这段版权信息，桑口～</p>
</blockquote>
<h4 id="Caffe安装"><a href="#Caffe安装" class="headerlink" title="Caffe安装"></a>Caffe安装</h4><p>首先还是简要的说一下Caffe在Ubuntu下的安装过程，具体安装过程如下：</p>
<ul>
<li><strong>安装前置依赖</strong><br>前置依赖项较多，好在Ubuntu下都可以通过apt下载安装，这个命令一阵复制粘帖吧：<figure class="highlight q"><table><tr><td class="code"><pre><div class="line">sudo apt-<span class="built_in">get</span> upgrade</div><div class="line">sudo apt-<span class="built_in">get</span> install -y build-essential cmake git pkg-config</div><div class="line">sudo apt-<span class="built_in">get</span> install -y libprotobuf-<span class="built_in">dev</span> libleveldb-<span class="built_in">dev</span> libsnappy-<span class="built_in">dev</span> libhdf5-serial-<span class="built_in">dev</span> protobuf-compiler</div><div class="line">sudo apt-<span class="built_in">get</span> install -y libatlas-base-<span class="built_in">dev</span></div><div class="line">sudo apt-<span class="built_in">get</span> install -y --no-install-recommends libboost-<span class="built_in">all</span>-<span class="built_in">dev</span></div><div class="line">sudo apt-<span class="built_in">get</span> install -y libgflags-<span class="built_in">dev</span> libgoogle-glog-<span class="built_in">dev</span> liblmdb-<span class="built_in">dev</span></div></pre></td></tr></table></figure>
</li>
</ul>
<p>然后安装OpenCV3.1,这个安装已经轻车熟路了。大家对此有和疑问的查看我这个<a href="www.lewisjin.coding.me">博客</a>，找到Ubuntu下安装OpenCV的教程进行安装。<br>接下来就简单了，不过为了防止后面导入caffe出问题，建议软链接一下这个东西：<br><figure class="highlight crystal"><table><tr><td class="code"><pre><div class="line">sudo ln -s /usr/<span class="class"><span class="keyword">lib</span>/<span class="title">x86_64</span>-<span class="title">linux</span>-<span class="title">gnu</span>/<span class="title">libboost_python</span>-<span class="title">py35</span>.<span class="title">so</span> /<span class="title">usr</span>/<span class="title">lib</span>/<span class="title">x86_64</span>-<span class="title">linux</span>-<span class="title">gnu</span>/<span class="title">libboost_python3</span>.<span class="title">so</span></span></div></pre></td></tr></table></figure></p>
<p>接着clone项目：<br><figure class="highlight crmsh"><table><tr><td class="code"><pre><div class="line">git <span class="keyword">clone</span> <span class="title">https</span>://github.com/BVLC/caffe.git</div></pre></td></tr></table></figure></p>
<p>然后在我的<a href="http://github.com/jinfagang/UbuntuScripts.git" target="_blank" rel="noopener">Repo</a>里面下载<code>Makefile</code>和<code>Makefile.config</code>来替换原有的。接着就是安装python依赖，这里不要按照其他教程里面的安装官方的caffe/python下的requirement.txt，instead，下载我的，然后运行：<br><figure class="highlight sql"><table><tr><td class="code"><pre><div class="line">sudo pip3 <span class="keyword">install</span> <span class="comment">--upgrade -r python/requirements_python3.txt</span></div></pre></td></tr></table></figure></p>
<p>后面的<code>requirements_python3.txt</code>也可以从上面的Repo下载。<br>接着直接安装就行了：<br><figure class="highlight vim"><table><tr><td class="code"><pre><div class="line"><span class="keyword">make</span> <span class="keyword">all</span> -<span class="keyword">j</span></div><div class="line"><span class="keyword">make</span> pycaffe</div></pre></td></tr></table></figure></p>
<p>测试一下是否安装成功：<br><figure class="highlight stylus"><table><tr><td class="code"><pre><div class="line">./data/mnist/get_mnist<span class="selector-class">.sh</span></div><div class="line">./examples/mnist/create_mnist<span class="selector-class">.sh</span></div><div class="line">./examples/mnist/train_lenet.sh</div></pre></td></tr></table></figure></p>
<p>如果成功你就会看到caffe在训练mnist了，大概几千次之后准确度就可以达到99%，非常好。</p>
<h4 id="Caffe生成并训练自己的数据集"><a href="#Caffe生成并训练自己的数据集" class="headerlink" title="Caffe生成并训练自己的数据集"></a>Caffe生成并训练自己的数据集</h4><p>本教程所有的代码，文件目录结构可以在我的github上下载，<a href="https://github.com/jinfagang/caffe_tiny5.git" target="_blank" rel="noopener">点这里</a>,下载之后你会看到一个完整的caffe训练架构，但是数据还得另外下载，我在这里只是上传了5类图片的样张图片，真正的数据集大概有600张的样子。下载地址为<a href="http://pan.baidu.com/s/1milroSS" target="_blank" rel="noopener">这里</a>。<br>接下来要生成caffe需要的数据了，步骤如下，大家先不要方，生成数据是整个过程最复杂的部分，不过这个部分也不是很麻烦，我精简一下步骤：</p>
<p><strong>Step 1</strong> Generate the image name file</p>
<p>Run caffe_path_gen.py in your terminal, just type:<br><figure class="highlight awk"><table><tr><td class="code"><pre><div class="line">python3 caffe_path_gen.py -train=<span class="regexp">/home/</span>jfg<span class="regexp">/caffe_tiny5/</span>tiny5<span class="regexp">/train -shuffle=True</span></div></pre></td></tr></table></figure></p>
<p>-shuffle is optional, because caffe can do this too.<br>In this tutorial we only have train data in image_data folder, we don’t have test image, so we just generate the train image path, and manully divide them into train and test. But if you have test data folder, you also can type:<br><figure class="highlight awk"><table><tr><td class="code"><pre><div class="line">python3 caffe_path_gen.py -train=<span class="regexp">/home/</span>jfg<span class="regexp">/Documents/</span>PythonSpace<span class="regexp">/caffe_tiny5/</span>tiny5<span class="regexp">/train -test=/</span>home<span class="regexp">/jfg/</span>Documents<span class="regexp">/PythonSpace/</span>caffe_tiny5<span class="regexp">/tiny5/</span>test -valid=<span class="regexp">/home/</span>jfg<span class="regexp">/Documents/</span>PythonSpace<span class="regexp">/caffe_tiny5/</span>tiny5<span class="regexp">/valid</span></div></pre></td></tr></table></figure></p>
<p>the valid data only generate image path without labels.<br>After this, you gonna have <strong>train.txt</strong>, <strong>words.txt</strong>.</p>
<p><strong>Step 2</strong> Split train.txt into to train.txt and test.txt</p>
<p><strong>Before split please remove the path just make it like this:</strong><br><figure class="highlight lsl"><table><tr><td class="code"><pre><div class="line">/flower/<span class="number">683.</span>jpg <span class="number">3</span></div><div class="line">/bus/<span class="number">334.</span>jpg <span class="number">0</span></div><div class="line">/bus/<span class="number">336.</span>jpg <span class="number">0</span></div><div class="line">/dinosaur/<span class="number">481.</span>jpg <span class="number">2</span></div><div class="line">/dinosaur/<span class="number">436.</span>jpg <span class="number">2</span></div><div class="line">/bus/<span class="number">327.</span>jpg <span class="number">0</span></div><div class="line">/elephant/<span class="number">595.</span>jpg <span class="number">4</span></div><div class="line">/bus/<span class="number">357.</span>jpg <span class="number">0</span></div><div class="line">/bus/<span class="number">393.</span>jpg <span class="number">0</span></div><div class="line">/bus/<span class="number">375.</span>jpg <span class="number">0</span></div><div class="line">/dinosaur/<span class="number">453.</span>jpg <span class="number">2</span></div><div class="line">/flower/<span class="number">654.</span>jpg <span class="number">3</span></div><div class="line">/dinosaur/<span class="number">491.</span>jpg <span class="number">2</span></div><div class="line">/bus/<span class="number">365.</span>jpg <span class="number">0</span></div><div class="line">/flower/<span class="number">636.</span>jpg <span class="number">3</span></div><div class="line">/flower/<span class="number">629.</span>jpg <span class="number">3</span></div><div class="line">/bus/<span class="number">347.</span>jpg <span class="number">0</span></div><div class="line">/bus/<span class="number">398.</span>jpg <span class="number">0</span></div><div class="line">/horse/<span class="number">761.</span>jpg <span class="number">1</span></div><div class="line">/elephant/<span class="number">560.</span>jpg <span class="number">4</span></div><div class="line">/dinosaur/<span class="number">449.</span>jpg <span class="number">2</span></div><div class="line">/elephant/<span class="number">531.</span>jpg <span class="number">4</span></div><div class="line">/horse/<span class="number">794.</span>jpg <span class="number">1</span></div><div class="line">/horse/<span class="number">743.</span>jpg <span class="number">1</span></div><div class="line">/elephant/<span class="number">586.</span>jpg <span class="number">4</span></div></pre></td></tr></table></figure></p>
<p>But stay with the class prefix</p>
<p><strong>Step 3</strong> Generate Caffe LMDB data file<br>First mkdir a <code>data</code> folder just inside the project directory, and place train.txt and test.txt into it.<br>And then open caffe_create_lmdb.sh and just edit the following two lines:<br><figure class="highlight groovy"><table><tr><td class="code"><pre><div class="line">TRAIN_DATA_ROOT=<span class="regexp">/home/</span>jfg<span class="regexp">/Documents/</span>PythonSpace<span class="regexp">/caffe_tiny5/</span>tiny5/train</div><div class="line">VAL_DATA_ROOT=<span class="regexp">/home/</span>jfg<span class="regexp">/Documents/</span>PythonSpace<span class="regexp">/caffe_tiny5/</span>tiny5/valid</div></pre></td></tr></table></figure></p>
<p>在这一步中，确保TRAIN_DATA_ROOT和上面的train.txt中的文件名能够组成完整的路径即可。</p>
<p><strong>Simply edit two lines, leave other along.</strong><br>Then type:<br><figure class="highlight armasm"><table><tr><td class="code"><pre><div class="line"><span class="keyword">bash </span>caffe_create_lmdb.sh</div></pre></td></tr></table></figure></p>
<p>Tip: Anything wrong, check you mkdir a data folder, and have train.txt and test.txt in it.<br>OK, after this, you gonna have two new folder in your data folder, that is:<br><code>caffe_train_lmdb</code><br><code>caffe_val_lmdb</code><br>this is what we need to feed into caffe net and it is nothing with your original image anymore! It’s complete and clean! Do not warry about path wrong anymore! Very nice!</p>
<p><strong>Step 4</strong> Generate Mean Binary File<br>This step is very easy, don’t change anything ,just type this in your terminal:<br><figure class="highlight armasm"><table><tr><td class="code"><pre><div class="line"><span class="keyword">bash </span>caffe_make_mean.sh</div></pre></td></tr></table></figure></p>
<p>And you gonna have <code>caffe_mean.binaryproto</code> file in your data folder.</p>
<h4 id="开始Caffe训练"><a href="#开始Caffe训练" class="headerlink" title="开始Caffe训练"></a>开始Caffe训练</h4><p>you already have your data, just finish 80% work. 20% to go. Next, we gonna using solver folder. In this folder we have a solver.prototxt and a train_test.prototxt.<br>solver.prototxt is the net pramas setting file.<br>train_test.prototxt is the net structure setting file and your lmdb data feed into net in here.<br><figure class="highlight dts"><table><tr><td class="code"><pre><div class="line"><span class="class">layer </span>&#123;</div><div class="line"><span class="symbol">  name:</span> <span class="string">"cifar"</span></div><div class="line"><span class="symbol">  type:</span> <span class="string">"Data"</span></div><div class="line"><span class="symbol">  top:</span> <span class="string">"data"</span></div><div class="line"><span class="symbol">  top:</span> <span class="string">"label"</span></div><div class="line">  <span class="class">include </span>&#123;</div><div class="line"><span class="symbol">    phase:</span> TRAIN</div><div class="line">  &#125;</div><div class="line">  <span class="class">transform_param </span>&#123;</div><div class="line"><span class="symbol">    mean_file:</span> <span class="string">"/home/jfg/caffe_tiny5/data/caffe_mean.binaryproto"</span></div><div class="line">  &#125;</div><div class="line">  <span class="class">data_param </span>&#123;</div><div class="line"><span class="symbol">    source:</span> <span class="string">"/home/jfg/caffe_tiny5/data/caffe_train_lmdb"</span></div><div class="line"><span class="symbol">    batch_size:</span> <span class="number">100</span></div><div class="line"><span class="symbol">    backend:</span> LMDB</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"><span class="class">layer </span>&#123;</div><div class="line"><span class="symbol">  name:</span> <span class="string">"cifar"</span></div><div class="line"><span class="symbol">  type:</span> <span class="string">"Data"</span></div><div class="line"><span class="symbol">  top:</span> <span class="string">"data"</span></div><div class="line"><span class="symbol">  top:</span> <span class="string">"label"</span></div><div class="line">  <span class="class">include </span>&#123;</div><div class="line"><span class="symbol">    phase:</span> TEST</div><div class="line">  &#125;</div><div class="line">  <span class="class">transform_param </span>&#123;</div><div class="line"><span class="symbol">    mean_file:</span> <span class="string">"/home/jfg/caffe_tiny5/data/caffe_mean.binaryproto"</span></div><div class="line">  &#125;</div><div class="line">  <span class="class">data_param </span>&#123;</div><div class="line"><span class="symbol">    source:</span> <span class="string">"/home/jfg/caffe_tiny5/data/caffe_val_lmdb"</span></div><div class="line"><span class="symbol">    batch_size:</span> <span class="number">100</span></div><div class="line"><span class="symbol">    backend:</span> LMDB</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>This 2 layer is data feed layer, so you have to change your data path in here.Make sure it correct.<br>Open solver.prototxt, find this 2 and edit it:<br><figure class="highlight avrasm"><table><tr><td class="code"><pre><div class="line"><span class="symbol">net:</span> <span class="string">"/home/jfg/caffe_tiny5/solver/caffenet_train_valid.prototxt"</span></div><div class="line"><span class="symbol">snapshot_prefix:</span> <span class="string">"/home/jfg/caffe_tiny5/model/caffenet"</span></div></pre></td></tr></table></figure></p>
<p>net: it is your train_test.prototxt file postion, snapshot_prefix is your save model path and prefix name, we place all saved models into a model_snapshot folder with prefix cifar10.</p>
<h4 id="训练"><a href="#训练" class="headerlink" title="训练"></a>训练</h4><p>只需要一行命令：<br><figure class="highlight armasm"><table><tr><td class="code"><pre><div class="line"><span class="keyword">bash </span>train_caffe.sh</div></pre></td></tr></table></figure></p>
<h4 id="重点来了，得到模型之后怎么预测"><a href="#重点来了，得到模型之后怎么预测" class="headerlink" title="重点来了，得到模型之后怎么预测"></a>重点来了，得到模型之后怎么预测</h4><p>这里我们使用python接口，直接上代码吧，实际上该的地方也不多：<br><figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="comment"># !/usr/bin/env python</span></div><div class="line"><span class="comment"># -*- coding: utf-8 -*-</span></div><div class="line"><span class="string">"""</span></div><div class="line">caffe_test.py</div><div class="line">http://www.lewisjin.coding.me</div><div class="line">~~~~~~~~~~~~~~~</div><div class="line">This script implement by Jin Fagang.</div><div class="line">: copyright: (c) 2017 Didi-Chuxing.</div><div class="line">: license: Apache2.0, see LICENSE for more details.</div><div class="line">"""</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> sys</div><div class="line"><span class="keyword">import</span> os</div><div class="line"><span class="keyword">import</span> cv2</div><div class="line"></div><div class="line">caffe_root = <span class="string">'/home/jfg/caffe/'</span></div><div class="line">sys.path.insert(<span class="number">0</span>, caffe_root + <span class="string">'python'</span>)</div><div class="line"><span class="keyword">import</span> caffe</div><div class="line"></div><div class="line"></div><div class="line">net_file = <span class="string">'/home/jfg/Documents/PythonSpace/caffe_tiny5/solver/caffenet_deploy.prototxt'</span></div><div class="line">caffe_model = <span class="string">'/home/jfg/Documents/PythonSpace/caffe_tiny5/solver/model/caffenet_iter_4500.caffemodel'</span></div><div class="line">mean_file = <span class="string">'/home/jfg/Documents/PythonSpace/caffe_tiny5/data/caffe_mean.binaryproto'</span></div><div class="line">print(<span class="string">'Params loaded!'</span>)</div><div class="line"></div><div class="line">caffe.set_mode_gpu()</div><div class="line">net = caffe.Net(net_file,</div><div class="line">                caffe_model,</div><div class="line">                caffe.TEST)</div><div class="line"></div><div class="line">mean_blob = caffe.proto.caffe_pb2.BlobProto()</div><div class="line">mean_blob.ParseFromString(open(mean_file, <span class="string">'rb'</span>).read())</div><div class="line">mean_npy = caffe.io.blobproto_to_array(mean_blob)</div><div class="line">a = mean_npy[<span class="number">0</span>, :, <span class="number">0</span>, <span class="number">0</span>]</div><div class="line"></div><div class="line">print(net.blobs[<span class="string">'data'</span>].data.shape)</div><div class="line">transformer = caffe.io.Transformer(&#123;<span class="string">'data'</span>: net.blobs[<span class="string">'data'</span>].data.shape&#125;)</div><div class="line">transformer.set_transpose(<span class="string">'data'</span>, (<span class="number">2</span>, <span class="number">0</span>, <span class="number">1</span>))</div><div class="line"></div><div class="line">transformer.set_mean(<span class="string">'data'</span>, a)</div><div class="line">transformer.set_raw_scale(<span class="string">'data'</span>, <span class="number">255.0</span>)</div><div class="line">transformer.set_channel_swap(<span class="string">'data'</span>, (<span class="number">2</span>, <span class="number">1</span>, <span class="number">0</span>))</div><div class="line"></div><div class="line">test_img = <span class="string">'elephant.jpeg'</span></div><div class="line">im = caffe.io.load_image(test_img)</div><div class="line">net.blobs[<span class="string">'data'</span>].data[...] = transformer.preprocess(<span class="string">'data'</span>, im)</div><div class="line"></div><div class="line">predict = net.forward()</div><div class="line">names = []</div><div class="line"><span class="keyword">with</span> open(<span class="string">'words.txt'</span>, <span class="string">'r+'</span>) <span class="keyword">as</span> f:</div><div class="line">    <span class="keyword">for</span> l <span class="keyword">in</span> f.readlines():</div><div class="line">        names.append(l.split(<span class="string">' '</span>)[<span class="number">1</span>].strip())</div><div class="line"></div><div class="line">print(names)</div><div class="line">prob = net.blobs[<span class="string">'prob'</span>].data[<span class="number">0</span>].flatten()</div><div class="line">print(<span class="string">'prob: '</span>, prob)</div><div class="line">print(<span class="string">'class: '</span>, names[np.argmax(prob)])</div><div class="line"></div><div class="line">img = cv2.imread(test_img)</div><div class="line">cv2.imshow(<span class="string">'Image'</span>, img)</div><div class="line">cv2.waitKey(<span class="number">0</span>)</div></pre></td></tr></table></figure></p>
<p>OK，本教程到此结束，欢迎乘坐本次老司机列车。这几天一直没有更新博客不是没有写，而且写了没有上传，还是得发时间整理整理，笔耕不啜才是一代文豪应该做的事情啊。</p>
<h4 id="近期计划"><a href="#近期计划" class="headerlink" title="近期计划"></a>近期计划</h4><p>这是一个花絮，最近我还是决定干点事情了，接下来我会研究一下如何用目标检测算法来识别交通信号灯，这个在无人驾驶领域是肯定会遇到的一个问题，具体来说它有几个难点：</p>
<ul>
<li>检测信号灯的位置</li>
</ul>
<p>要检测信号灯就需要训练图片，标注信号灯的方框，这个好办，现有的网络可以做到很好，接下来有个问题就是第二个点。</p>
<ul>
<li>检测信号等每个路口转向的信号等情况</li>
</ul>
<p>比如我检测到了信号灯，但是这个信号灯是左转通行还是右转通行呢？是左转通行直行禁止右转禁止还是其他情况呢？而且我检测算法可能在一个视角中检测多个红绿灯，但是我真正感兴趣的只是我前方的红绿灯，那么如何来却分识别出来的红绿灯也是个问题。</p>
<p>这两个问题如何解决还得看数据标注的情况。</p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '骚年，加个好友打赏一下啊，现在连泡面都吃不起了啊', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://i.loli.net/2017/09/27/59cb048ba6838.jpeg',
  alipayImage: 'https://i.loli.net/2017/09/27/59cb049cd0951.jpeg'
});
</script>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8zMDA5MC82NjQ1">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/深度学习/">深度学习</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/02/02/Django-6-Django-nginx-gunicorn-搭建web生产环境教程/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          Django 6 Django，nginx，gunicorn搭建web生产环境教程
        
      </div>
    </a>
  
  
    <a href="/2017/02/01/2017-1-17-Shell编程从入门到放弃系列/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Shell program from newbie to give up series</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Caffe训练自己的数据集并用Python接口预测"><span class="nav-number">1.</span> <span class="nav-text">Caffe训练自己的数据集并用Python接口预测</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Caffe安装"><span class="nav-number">1.1.</span> <span class="nav-text">Caffe安装</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Caffe生成并训练自己的数据集"><span class="nav-number">1.2.</span> <span class="nav-text">Caffe生成并训练自己的数据集</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#开始Caffe训练"><span class="nav-number">1.3.</span> <span class="nav-text">开始Caffe训练</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#训练"><span class="nav-number">1.4.</span> <span class="nav-text">训练</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#重点来了，得到模型之后怎么预测"><span class="nav-number">1.5.</span> <span class="nav-text">重点来了，得到模型之后怎么预测</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#近期计划"><span class="nav-number">1.6.</span> <span class="nav-text">近期计划</span></a></li></ol></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://www.luoli-luoli.com/" target="_blank">萝莉萝莉</a> and <a href="http://www.luoli-luoli.com/sia" target="_blank">Sia</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2017 - 2018 Jin Tian All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
    <a href="http://luoli-luoli.com/chat" class="mobile-nav-link">Chat</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            Jin Tian
          </div>
          <div class="panel-body">
            Copyright © 2018 Jintian All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>